<?php

namespace app\health\library;

use Exception;
use think\exception\Handle;

class SendSms extends Handle
{
    // private $userid = 'wxe2c60971a554e225';
    // private $pwd = 'c8ae5720741ffb4dfaac7bfc454d1e23';
    private static $apikey = '00e6e1637e9c310d98ef640f32e4e635';
    // private static $apikeys = 'b1798653a01413f8ada478a367e0b964';
    private static $BaseUrl = 'http://175.25.21.202:8901/sms/v2/std/single_send';
    public static $ERROR_310099 = -310099; //http请求失败错误码

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 密码加密
     * $userid：用户账号
     * $pwd：用户密码
     */
    public function encrypt_pwd($userid, $pwd)
    {
        try {
            $char = '00000000'; //固定字符串
            $time = date('mdHis', time()); //时间戳
            $pwd = md5($userid . $char . $pwd . $time); //拼接字符串进行加密
            return array('pwd' => $pwd, 'time' => $time);
        } catch (Exception $e) {
            print_r($e->getMessage());  //输出捕获的异常消息
        }
    }

    /**
     * 短信内容加密
     * $content：短信内容
     */
    public static function encrypt_content($content)
    {
        try {
            return urlencode(iconv('UTF-8', 'GBK', $content)); //短信内容转化为GBK格式再进行urlencode格式加密
        } catch (Exception $e) {
            print_r($e->getMessage());  //输出捕获的异常消息
        }
    }

    /**
     * 短连接请求方法
     * $url：请求地址
     * $post_data：请求数据
     */
    private static function connection($url, $post_data)
    {
        try {
            $attributes = array('Accept:text/plain;charset=utf-8', 'Content-Type:application/json', 'charset=utf-8', 'Expect:', 'Connection: Close'); //请求属性
            $ch = curl_init(); //初始化一个会话
            /* 设置验证方式 */
            curl_setopt($ch, CURLOPT_HTTPHEADER, $attributes); //设置访问
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //设置返回结果为流
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //设置请求超时时间
            curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置响应超时时间
            /* 设置通信方式 */
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //使用urlencode格式请求

            $result = curl_exec($ch); //获取返回结果集
            $result = preg_replace('/\"msgid":(\d{1,})./', '"msgid":"\\1",', $result); //正则表达式匹配所有msgid转化为字符串
            $result = json_decode($result, true); //将返回结果集json格式解析转化为数组格式
            if (curl_errno($ch) !== 0) //网络问题请求失败
            {
                $result['result'] = self::$ERROR_310099;
                curl_close($ch); //关闭请求会话
                return $result;
            } else {
                $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if ($statusCode != 200 || !isset($result['result'])) //域名问题请求失败或不存在返回结果
                {
                    $result = ''; //清空result集合
                    $result['result'] = self::$ERROR_310099;
                }
                curl_close($ch); //关闭请求会话
                return $result;
            }
        } catch (Exception $e) {
            print_r($e->getMessage()); //输出捕获的异常消息
            $result['result'] = self::$ERROR_310099; //返回http请求错误代码
            return $result;
        }
    }

    /**
     * 单条发送
     * @param [type] $data
     * @Author 张旭鹏
     * @DateTime 2023-07-04
     */
    public static function singleSend($data)
    {
        try {
            $data['apikey'] = self::$apikey;
            $data['content'] = self::encrypt_content($data['content']); //短信内容进行urlencode加密
            $post_data = json_encode($data); //将数组转化为JSON格式
            $result = self::connection(self::$BaseUrl, $post_data); //根据请求类型进行请求
            return $result; //返回请求结果
        } catch (Exception $e) {
            print_r($e->getMessage());  //输出捕获的异常消息
        }
    }
}
